
//
// 120524 - AH - (Aura) Created.
// 120626 - AH - (Huset) Took CE_ID, SEMANTIC_EFFECT_NAME, 
//               SEMANTIC_EFFECT_ACTION.
//

package org.soekrat.server.servlets.semanticeffects;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.ConversationElement;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements request to delete a semantic effect.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class DeleteSemanticEffectHttpServlet 
  extends AbstractSoekratHttpServlet 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DeleteSemanticEffectHttpServlet(){}

  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
    }
    else
    {
      if (!validate(res, def, req)) return;
      
      long ceId=def.fetch(CE_ID.name(), req).getLongValue();
      String name=def.fetch(SEMANTIC_EFFECT_NAME.name(), req).getStringValue();
      String action=def.fetch(SEMANTIC_EFFECT_ACTION.name(), req)
        .getStringValue();
      
      SoekratDAO dao=new SoekratDAOFactory().create();
      
      ConversationElement ce=dao.getConversationElement(ceId);
      if (ce==null)
      {
        respond(res, "no conversation element for ce-id : "+ceId);
        return;
      }
      
      dao.deleteSemanticEffect(ce, name, action);
      
      respond(res, "ok");
    }
  }

  @Override
  public HttpRequestDef getHttpRequestDef() 
  {
    return new HttpRequestDef("/soekrat_server/delete-semantic-effect")
      .desc("Deletes A Semantic Effect").parameter(CE_ID)
      .parameter(SEMANTIC_EFFECT_NAME).parameter(SEMANTIC_EFFECT_ACTION);
  }
  
}
